package cn.zb.zbrhback.auth.controller;

import cn.zb.zbrhback.auth.annotation.ZBPermission;
import cn.zb.zbrhback.auth.domain.Role;
import cn.zb.zbrhback.auth.domain.RoleQuery;
import cn.zb.zbrhback.auth.service.IRoleService;
import cn.zb.zbrhback.basic.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/role")
@Api(value = "角色Api",tags = "角色管理")
@ZBPermission(name = "角色管理",desc = "角色管理")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    /**
     * 新增/修改
     * */
    @PutMapping
    @ApiOperation(value = "新增或修改接口",notes = "有id为修改，否则为新增")
    @ZBPermission(name = "新增/修改",desc = "新增或修改的权限")
    public AjaxResult addorUpdate(@RequestBody Role role){
        try {
            if(Objects.nonNull(role.getId())){
                roleService.update(role);
            }else {
                roleService.add(role);
            }
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("新增/修改失败"+e.getMessage());
        }
    }

    /**
     * 根据id删除
     * */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除接口",notes = "id必传")
    @ZBPermission(name = "根据id删除",desc = "根据id删除")
    public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            roleService.deleteById(id);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("根据id删除失败"+e.getMessage());
        }
    }

    /**
     * 根据id查询
     * */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询接口",notes = "id必传")
    @ZBPermission(name = "根据id查询",desc = "根据id查询")
    public AjaxResult getById(@PathVariable("id") Long id){
        try {
            Role role = roleService.getById(id);
            return AjaxResult.me().setData(role);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("根据id查询失败"+e.getMessage());
        }
    }

    /**
     * 查询所有
     * */
    @GetMapping
    @ApiOperation(value = "查询所有",notes = "不需要参数")
    @ZBPermission(name = "查询所有",desc = "查询所有")
    public AjaxResult getAll(){
        try {
            List<Role> roles = roleService.getAll();
            return AjaxResult.me().setData(roles);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询全部失败"+e.getMessage());
        }
    }

    /**
     * 批量删除
     * */
    @PatchMapping
    @ApiOperation(value = "批量删除",notes = "需要id数组")
    @ZBPermission(name = "批量删除",desc = "批量删除")
    private AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            roleService.batchDelete(ids);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("批量删除失败"+e.getMessage());
        }
    }

    /**
     * 分页查询
     * */
    @PostMapping
    @ApiOperation(value = "分页条件查询",notes = "当前页，每页条数，参数可有可无")
    @ZBPermission(name = "分页条件查询",desc = "分页条件查询")
    public AjaxResult pageQuery(@RequestBody RoleQuery roleQuery){
        try {
            PageResult<Role> roles = roleService.findPage(roleQuery);
            return AjaxResult.me().setData(roles);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("分页条件查询失败"+e.getMessage());
        }
    }

    @GetMapping("/permissionsn/{roleId}")
    @ApiOperation(value = "获取角色权限sn",notes = "需要参数角色id")
    @ZBPermission(name = "获取角色权限sn",desc = "获取角色权限sn")
    public AjaxResult getSnByRoleId(@PathVariable("roleId") Long roleId){
        try {
            List<String> sns = roleService.getSnByRoleId(roleId);
            return AjaxResult.me().setData(sns);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取角色权限sn失败"+e.getMessage());
        }
    }

    @PostMapping("/permission")
    @ApiOperation(value = "保存角色权限sn",notes = "需要参数角色roleId和权限数组")
    @ZBPermission(name = "保存角色权限sn",desc = "保存角色权限sn")
    public AjaxResult saveRolePermission(@RequestBody PermissionDTO permissionDTO){
        try {
            roleService.saveRolePermission(permissionDTO);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存角色权限sn失败"+e.getMessage());
        }
    }

    @GetMapping("/getMenuId/{id}")
    @ApiOperation(value = "获取角色拥有的菜单id",notes = "需要角色id参数")
    @ZBPermission(name = "获取角色拥有的菜单id",desc = "获取角色拥有的菜单id")
    public AjaxResult getRoleMenuById(@PathVariable("id") Long id){
        try {
            List<Long> ids = roleService.getRoleMenuById(id);
            return AjaxResult.me().setData(ids);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取角色拥有的菜单id失败"+e.getMessage());
        }
    }

    @PostMapping("/menu")
    @ApiOperation(value = "保存角色菜单id关系",notes = "参数角色id与菜单id数组")
    @ZBPermission(name = "保存角色菜单id关系",desc = "保存角色菜单id关系")
    public AjaxResult saveMenu(@RequestBody MenuDTO menuDTO){
        try {
            roleService.saveMenu(menuDTO);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存角色菜单id关系失败"+e.getMessage());
        }
    }

    @GetMapping("/ids")
    @ApiOperation(value = "获取所有角色的sn",notes = "不需要参数")
    @ZBPermission(name = "获取所有角色的sn权限",desc = "获取所有角色的sn权限")
    public AjaxResult getAllRoleSn(){
        try {
            List<Long> ids = roleService.getAllRoleId();
            return AjaxResult.me().setData(ids);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有角色的sn失败"+e.getMessage());
        }
    }

    @GetMapping("/getRole/{id}")
    @ApiOperation(value = "获取用户已有角色",notes = "需要参数用户id")
    @ZBPermission(name = "获取用户已有角色权限",desc = "获取用户已有角色权限")
    public AjaxResult getSnById(@PathVariable("id") Long id){
        try {
            List<Long> ids = roleService.getSnById(id);
            return AjaxResult.me().setData(ids);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取用户已有角色失败"+e.getMessage());
        }
    }

    @PostMapping("/addRole")
    @ApiOperation(value = "保存与员工角色id关系",notes = "参数员工id与角色id数组")
    @ZBPermission(name = "保存与员工角色id关系权限",desc = "保存与员工角色id关系权限")
    public AjaxResult saveRole(@RequestBody RoleDTO roleDTO){
        try {
            roleService.saveRole(roleDTO);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存角色菜单id关系失败"+e.getMessage());
        }
    }
}
